import {accountInfo, accountList, addAccount, changeAccount, loginRequest} from "@/api/user";
import store from "@/store";

// 登录请求
export function sendLoginRequest(option){
	return new Promise((resolve,reject) => {
		loginRequest(option).then((response) => {
			let data = response.data
			resolve(data)
			// console.log(data)
		}).catch((error) => {
			reject(error)
		})
	})
}

// 修改账户信息
export function sendChangeAccount(option) {
	return new Promise((resolve, reject) => {
		changeAccount(option).then((response) => {
			let data = response.data
			resolve(data)
		}).catch((error) =>{
			reject(error)
		})
	})
}
// 增加账户信息
// export function sendAddAccount(option) {
// 	return new Promise((resolve, reject) => {
// 		addAccount(option).then((response) => {
// 			let data = response.data
// 			resolve(data)
// 		}).catch((error) =>{
// 			reject(error)
// 		})
// 	})
// }
// 获取账户列表
export function getAccountList(uid) {
	return new Promise((resolve, reject) => {
		accountList(uid).then((response) => {
			let data = response.data
			resolve(data)
		}).catch((error) =>{
			reject(error)
		})
	})
}
// 获取单个用户信息
// export function getAccountInfo(userMap) {
// 	return new Promise((resolve, reject) => {
// 		accountInfo(userMap).then((response) => {
// 			let data = response.data
// 			resolve(data)
// 		}).catch((error) =>{
// 			reject(error)
// 		})
// 	})
// }
// 存入登录用户信息
export function saveUserInfo(userInfo){
	store.dispatch('setUsername',userInfo.username)
	store.dispatch('setUid',userInfo.uid)
	store.dispatch('setRoleId',userInfo.roleId)
	store.dispatch('setIsphoto',userInfo.isPhoto)
	store.dispatch('setIsconfigue',userInfo.isConfigue)
	store.dispatch('setLoginStatus',true)
}
// 移除登录用户信息
export function removeUserInfo(){
	store.dispatch('setUid',null)
	store.dispatch('setUsername',null)
	store.dispatch('setPassword',null)
	store.dispatch('setEmail',null)
	store.dispatch('setRoleId',0)
	store.dispatch('setLoginStatus',false)
}

export function storeUserInfo (AuthReq) {
	localStorage.setItem('username', AuthReq.username)
	localStorage.setItem('password', AuthReq.password)
	localStorage.setItem('uid', AuthReq.uid)
	localStorage.setItem('roleId', AuthReq.roleId)
	localStorage.setItem('isPhoto', AuthReq.isPhoto)
	localStorage.setItem('isConfigue', AuthReq.isConfigue)
}

export function removeStoreUserInfo () {
	localStorage.removeItem('username')
	localStorage.removeItem('password')
	localStorage.removeItem('uid')
	localStorage.removeItem('roleId')
	localStorage.removeItem('isPhoto')
	localStorage.removeItem('isConfigue')
}
